package cn.csq.lambda;

import cn.csq.sort.Apple;
import com.sun.org.apache.bcel.internal.generic.LSTORE;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author csq
 * @version 1.0.0
 * @ClassName MainTest.java
 * @Description TODO
 * @createTime 2022年02月13日 21:38:00
 */
public class MainTest {

    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor())) {
                result.add(apple);
            }

        }
        return result;
    }

    public static List<Apple> filterHeavyApples(List<Apple> inventory) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.getWeight()>50) {
                result.add(apple);
            }

        }
        return result;
    }


    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 50;
    }

    static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        ArrayList<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        ArrayList<Apple> inventory = new ArrayList<>();
        Apple apple1 = new Apple();
        apple1.setWeight(10);
        Apple apple2 = new Apple();
        apple2.setWeight(2);
        inventory.add(apple1);
        inventory.add(apple2);

        //谓词  MainTest::isGreenApple 接受参数Apple并返回一个boolean  谓词Predicate 在数学中常常用来代表一个类似函数的东西
        //接受一个参数的值 并返回true 和 false
        filterApples(inventory, MainTest::isGreenApple);
        filterApples(inventory, MainTest::isHeavyApple);

        inventory.stream().filter(i -> i.getWeight() > 50);

        filterApples(inventory,(Apple a)->a.getWeight() > 50);
        filterApples(inventory, (Apple a) -> a.getWeight() > 50 || "green".equals(a.getColor()));


        //顺序处理
        inventory.stream().filter((Apple a) -> a.getWeight() > 100).collect(Collectors.toList());
        //并行处理
        inventory.parallelStream().filter((Apple a) -> a.getWeight() > 100).collect(Collectors.toList());




    }
}
